#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>



union semval
{
	int dataval;
};

/*
   int sem_init(int semid,int semnum,int val)
   {
   union semval myval;
   myval.dataval=val;
   semctl(semid,semnum,SETVAL,myval)

   return 0;	
   }
   */
int sem_p(int semid,int semnum)
{
	struct sembuf mybuf;
	mybuf.sem_num=semnum;
	mybuf.sem_op=-1;
	mybuf.sem_flg=0;

	semop(semid,&mybuf,1);

	return 0;
}

int sem_v(int semid,int semnum)
{
	struct sembuf mybuf;
	mybuf.sem_num=semnum;
	mybuf.sem_op=1;
	mybuf.sem_flg=0;

	semop(semid,&mybuf,1);

	return 0;
}


int main(int argc, const char *argv[])
{
	key_t key1=ftok("../messagequeue/messagequeue1.c",'a');
	key_t key2=ftok("../messagequeue/messagequeue2.c",'b');

	int shmid=shmget(key1,4096,IPC_CREAT|0664);
	int semid=semget(key2,2,IPC_CREAT|0664);

	char *buf=(char *)shmat(shmid,NULL,0);
	while(1)
	{
		printf("waiting \n");
		sem_p(semid,1);
		printf("%s\n",buf);
		sem_v(semid,0);
	}


	return 0;
}
